﻿@model Cxuu.Models.EditArticleStatement
@{
    var cateInfo = ViewBag.cateInfo as ArticleCate;
}

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>@ViewData["Title"]</title>
    <link rel="stylesheet" href="~/lib/layui/css/layui.css">
    <link rel="stylesheet" href="~/admin/css/main.css">
    <style>
        html {
            zoom: 100%;
            width: 95%;
            margin: 5px auto;
            height: auto;
        }
    </style>
</head>
<body>
    <div class="layui-fluid" style="margin-bottom:50px;">
        <div class="layui-form">
            <form class="layui-form" action="" lay-filter="FormEdit" enctype="multipart/form-data">
                <input asp-for="Cid" type="hidden">
                <div class="layui-form-item">
                    <label class="layui-form-label required" style="color:red !important;font-weight:bold;font-size:14px;">@cateInfo.Name</label>
                    <div class="layui-input-block">
                        <input type="text" asp-for="Title" lay-verify="required" lay-reqtext="工作内容不能为空" placeholder="请输入工作内容" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    @if (!string.IsNullOrEmpty(cateInfo.ObjectId1))
                    {
                        <div class="layui-inline">
                            <label class="layui-form-label required">@cateInfo.ObjectId1</label>
                            <div class="layui-input-block">
                                <input type="text" asp-for="ObjectId1" value="" class="layui-input">
                            </div>
                        </div>
                    }
                   
                    @if (!string.IsNullOrEmpty(cateInfo.ObjectId2))
                    {
                        <div class="layui-inline">
                            <label class="layui-form-label required">@cateInfo.ObjectId2</label>
                            <div class="layui-input-block">
                                <input type="text" asp-for="ObjectId2" value="" class="layui-input">
                            </div>
                        </div>
                    }
                </div>
                <div class="layui-form-item" style="display:none;">
                    <label asp-for="Clercator" class="layui-form-label required"></label>
                    <div class="layui-input-block" style="width:515px;">
                        <input type="text" asp-for="Clercator" value="" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label asp-for="Img" class="layui-form-label required"></label>
                    <div class="layui-input-block" style="width:515px;">
                        <input type="text" asp-for="Img" value="" class="layui-input" id="imageval">
                        <div class="layui-upload-list" style="width:420px;" id="photopre">
                            <p id="errorText"></p>
                        </div>
                        @if (await Authorizer.AuthView(PermissionKeys.Upload_Upload))
                        {
                            <button type="button" class="layui-btn" id="thumimg">
                                <i class="layui-icon">&#xe67c;</i>上传图片
                            </button>

                            <div style="width: 95px;">
                                <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="uploadText">
                                    <div class="layui-progress-bar" lay-percent=""></div>
                                </div>
                            </div>
                        }
                    </div>
                </div>

                @if (await Authorizer.AuthView(PermissionKeys.Upload_Upload))
                {
                    <div class="layui-form-item">
                        <label class="layui-form-label required">附件</label>
                        <div class="layui-input-block">
                            <div id="attval" class="cxuu_attmentupload"></div>
                            <input type="hidden" asp-for="Attments" value="" class="layui-input" id="Att">
                            <button type="button" class="layui-btn" id="attmentsUpload">
                                <i class="layui-icon ">&#xe608;</i>上传附件
                            </button>
                            <tip>支持多个附件上传，单个附件不超过 @ViewBag.fileSize KB！ 支持格式：@ViewBag.fileExts </tip>
                        </div>
                    </div>
                }

                <div class="layui-form-item layui-form-text">
                    <label asp-for="Remarks" class="layui-form-label"></label>
                    <div class="layui-input-block">
                        <textarea name="Remarks" class="layui-textarea" placeholder="请输入备注"></textarea>
                    </div>
                </div>

                <div class="layui-form-item" style="text-align:center;">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn" id="saveBtn"><i class="layui-icon">&#xe605;</i>提交</button>
                </div>
                <input asp-for="Id" type="hidden">

            </form>
        </div>
    </div>
    <script src="~/lib/jquery.min.js?v=1.12.4"></script>
    <script src="~/lib/layui/layui.js"></script>
    <script src="~/lib/cxuu-main.js"></script>
    <script>
        layui.define(function () {
            var form = layui.form, layer = layui.layer, $ = layui.jquery, upload = layui.upload, element = layui.element;
            var laydate = layui.laydate;

            form.render();

            laydate.render({
                elem: '#GreatTime'
                , type: 'datetime'
                , value: new Date()
                , isInitValue: true
            });

            $("#imageval").hide();
            var editId = @ViewBag.id+1;
            //console.log(editId);
            if (editId > 1) {
                $.getJSON("/Admin/Statement/AddOrEdit?id=@ViewBag.id&edit=1", function (res) {
                    if (res.status == 1) {
                        var data = res.data;
                        form.val('FormEdit', {
                            Id: data.id,
                            Cid: data.cid,
                            Title: data.title,
                            Attments: data.attments,
                            ObjectId1: data.objectId1,
                            ObjectId2: data.objectId2,
                            Clercator: data.clercator,
                            Remarks: data.remarks,
                            Img: data.img,
                        });
                        if (data.img != null) $('#photopre').html('<img src="' + data.img + '" width="120" height="80" >');
                        $.each(res.attments, function (index, value) {
                            $('#attval').append('<span id="attidd' + value.id + '" class="layui-badge layui-bg-blue"> ID：' + value.id + ' 文件名：' + value.filename +
                                ' <a href="javascript:;" onclick="attColse(' + value.id + ')" ><i class="layui-icon layui-icon-close"></i></a></span>');
                        });
                    } else {
                        layer.msg("数据加载失败");
                    }

                })
            } else {
                form.val('FormEdit', {
                    //  Cid: @ViewBag.cid,
                });
            }

            var uploadInst = upload.render({
                elem: '#attmentsUpload'
                , url: '/Admin/Upload/Upload?type=layui&source=Docreceived'
                , before: function (obj) {
                    element.progress('upload-att', '0%'); //进度条复位
                    layer.msg('上传中', { icon: 16, time: 0 });
                }
                , size: "@ViewBag.fileSize"
                , accept: 'file'
                , acceptMime: 'file/*'
                //, exts: 'zip|rar|7z|doc|docx|xlsx|xls|'
                , exts: "@ViewBag.fileExts".replace(/./g, '')
                , done: function (res) {
                    if (res.status == 1) {
                        $('#attval').append('<span id="attidd' + res.pkey + '" class="layui-badge layui-bg-blue"> ID：' + res.pkey +
                            ' 文件名：' + res.data.filename + ' <a href="javascript:;" onclick="attColse(' + res.pkey + ')" ><i class="layui-icon layui-icon-close"></i></a></span>');
                        var input = $("#Att").val();
                        if (input != "" && input != undefined) {
                            $("#Att").val($("#Att").val() + "," + res.pkey);
                        } else {
                            $('#Att').val(res.pkey);
                        }
                        layer.msg(res.info);
                    } else {
                        layer.msg("上传失败！");
                    }
                }
                //进度条
                , progress: function (n, elem, e) {
                    element.progress('upload-att', n + '%');
                    if (n == 100) {
                        layer.msg('上传完毕', { icon: 1 });
                    }
                }
            });

            //常规使用 - 普通图片上传
            var uploadImg = upload.render({
                elem: '#thumimg'
                , url: '/Admin/Upload/Upload?type=layuiImg&source=Docreceived'
                , before: function (obj) {
                    element.progress('uploadText', '0%'); //进度条复位
                    layer.msg('上传中', { icon: 16, time: 0 });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.status != 1) {
                        return layer.msg('上传失败');
                    }
                    //上传成功的一些操作
                    $('#imageval').val(res.data.url);
                    $('#photopre').html('<img src="' + res.data.url + '" width="120" height="80" >');
                }
                , error: function () {
                    //失败状态，并实现重传
                    var errorText = $('#imageval');
                    imageval.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoimagevalText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
                //进度条
                , progress: function (n, elem, e) {
                    element.progress('uploadText', n + '%');
                    if (n == 100) {
                        layer.msg('上传完毕', { icon: 1 });
                    }
                }
            });

            var dataPost = function (url, data) {
                $.post(url, data, function (res) {
                    if (res.status == 1) {
                        $("#saveBtn").hide();
                        layer.msg(res.msg, { icon: 1, time: 500 }, function () {
                            parent.layui.table.reloadData('tableReload');
                            parent.layer.closeAll();
                        });
                    } else
                        layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
                });
            }

            $('#saveBtn').click(function () {
                var field = form.val('FormEdit');
                if (editId == 1) {
                    field.Id = 0;
                    dataPost('/Admin/Statement/CreatePost', field);
                }
                else {
                    dataPost('/Admin/Statement/EditPost', field);
                }
                return false;
            });
        });



        function attColse(id) {
            layer.confirm('真的删除这个附件吗？', { icon: 3, title: '警告' }, function (index) {
                $.post('/Admin/Attment/Delete', { id: id }, function (res) {
                    if (res.status == 1) {
                        layer.msg("附件: " + res.msg);
                        $('#attidd' + id).remove();
                        var input = $("#Att").val();
                        var newarr = input.split(",");
                        newarr.splice($.inArray("" + id + "", newarr), 1);
                        $('#Att').val(newarr);
                    } else {
                        layer.msg("附件: " + res.msg + "请查看是否有删除附件权限");
                    }
                });
                layer.close(index);
            });
        }

    </script>
</body>
</html>